package cDB

import (
	"gitee.com/csingo/cComponents"
	"gitee.com/csingo/cLog"
)

type DBComponent struct{}

func (i *DBComponent) Inject(instance any) bool {
	return false
}

func (i *DBComponent) InjectConf(config cComponents.ConfigInterface) bool {
	if config.ConfigName() == DatabaseConfigName {
		database_config = config.(*DatabaseConf)
		return true
	}

	return false
}

func (i *DBComponent) Load() {
	for name, driver := range database_config.Connections {
		if container.Has(name) {
			continue
		}
		connector, err := driver.NewConnector(name)
		if err != nil {
			cLog.WithContext(nil, map[string]any{
				"source": "cDB.DBComponent.Load",
				"name":   name,
				"driver": driver,
				"err":    err.Error(),
			}, []cLog.Option{
				cLog.IncludePaths{},
			}).Fatal("数据库连接异常")
		}
		if container.Is(connector) {
			container.Save(connector)
		}
	}
}

func (i *DBComponent) Listen() []*cComponents.ConfigListener {
	return []*cComponents.ConfigListener{}
}

var Component = &DBComponent{}
